package com.hna.eking.Kafka;

import java.util.Arrays;
import java.util.Properties;

import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.apache.kafka.clients.consumer.ConsumerRecords;
import org.apache.kafka.clients.consumer.KafkaConsumer;
import org.apache.kafka.clients.producer.KafkaProducer;
import org.apache.kafka.clients.producer.ProducerRecord;
import org.apache.kafka.common.serialization.StringSerializer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.hna.eking.ZkUtils.MainTest;

public class KafkaUtils {
	private Properties props;
	private String Host = "localhost:9092";
	private String TransID = "0000000000001";
	private String KeySerializer = "org.apache.kafka.common.serialization.StringSerializer";
	private String ValueSerializer = "org.apache.kafka.common.serialization.StringSerializer";
	private static final Logger logger = LoggerFactory.getLogger(KafkaUtils.class);
	public void InitKafka(){
		
	}
	
	
	
	public boolean Consumer(String Msg){
		KafkaConsumer<String, String> consumer = new KafkaConsumer<>(props);
		consumer.subscribe(Arrays.asList("test", "kafkatst"));
		while (true) {
			ConsumerRecords<String, String> records = consumer.poll(100);
			for (ConsumerRecord<String, String> record : records)
				System.out.printf("offset = %d, key = %s, value = %s%n", record.offset(), record.key(), record.value());
			break;
		}
		return true;
	}
	
	public String Producer(String Topic, String Key, String Value){
		KafkaProducer<String, String> producer = new KafkaProducer<>(props, new StringSerializer(),
				new StringSerializer());
		try {
//			producer.beginTransaction();
			
			producer.send(new ProducerRecord<>("streams", "counts",  Value));
//			producer.commitTransaction();
		} catch (Exception e) {
			e.printStackTrace();
			logger.error("abortTransaction begin ");
		}finally{
			logger.debug("procedure closed");
			producer.close();
		}
		
		return "";
	}



	public String getHost() {
		return Host;
	}



	public void setHost(String host) {
		Host = host;
	}



	public String getTransID() {
		return TransID;
	}



	public void setTransID(String transID) {
		TransID = transID;
	}



	public String getKeySerializer() {
		return KeySerializer;
	}



	public void setKeySerializer(String keySerializer) {
		KeySerializer = keySerializer;
	}



	public String getValueSerializer() {
		return ValueSerializer;
	}



	public void setValueSerializer(String valueSerializer) {
		ValueSerializer = valueSerializer;
	}
	
	
}
